//
//  LZDescriptionTableViewCell.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/20.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZDescriptionTableViewCell.h"
#import "Masonry.h"
#import "LZCouponListModel.h"
#import "PlaceholderTextView.h"

@interface LZDescriptionTableViewCell() // <UITextViewDelegate>

@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) PlaceholderTextView * textView;

//@property (nonatomic, strong) UITextView * textView;
//@property (nonatomic, strong) UILabel * placeHolderLabel;

@property (nonatomic, strong) UILabel * numberLabel;
@property (assign, nonatomic) BOOL isHaveContent;

@end

@implementation LZDescriptionTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self configView];
        [self addAutoLayoutToCell];
    }
    return self;
}

- (void)configView {
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.textView];
//    [self.textView addSubview:self.placeHolderLabel];
//    [self.textView setValue:self.placeHolderLabel forKey:@"_placeholderLabel"];
    [self.contentView addSubview:self.numberLabel];
    
    WS(weakSelf);
    
    [self.textView addMaxTextLengthWithMaxLength:0 andEvent:^(PlaceholderTextView *textView) {
        DLog(@"*********%@",textView.text);
        if (textView.text.length > 1000) {
            textView.text = [textView.text substringToIndex:1000];
        }
//        weakSelf.labNum.text = [NSString stringWithFormat:@"%ld",(unsigned long)text.text.length];
        [weakSelf setNumberlabelStringForNum:(unsigned long)textView.text.length];
//        if (weakSelf.isHaveContent != text.text.length) {
//            weakSelf.isHaveContent = text.text.length;
//            if (weakSelf.haveTextBlock) {
//                weakSelf.haveTextBlock(weakSelf.isHaveContent);
//            }
//        }
    }];
    
    [self.textView addTextViewBeginEvent:^(PlaceholderTextView * textView) {
        DLog(@"begin----%@",textView.text);
    }];
    
    [self.textView addTextViewEndEvent:^(PlaceholderTextView * textView) {
        DLog(@"end----%@",textView.text);
        if (weakSelf.textViewEditDidEndBlock) {
            weakSelf.textViewEditDidEndBlock(textView.text, textView.tag, weakSelf.indexPath);
        }
    }];
}

- (void)addAutoLayoutToCell {
    
    WS(weakSelf);
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(20);
    }];
    
    [self.textView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.titleLabel.mas_bottom).with.offset(15);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.bottom.mas_equalTo(-25);
    }];
    
    [self.numberLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.textView.mas_bottom).with.offset(5);
        make.trailing.mas_equalTo(-15);
        make.height.mas_equalTo(10);
        make.width.mas_lessThanOrEqualTo(50);
    }];
}

- (UILabel *)titleLabel {
    
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = UIColorFromRGB(0x333333, 1);
    }
    
    return _titleLabel;
}


- (UITextView *)textView {
    
    if (_textView == nil) {
        _textView = [[PlaceholderTextView alloc] initWithFrame:CGRectZero];
        [_textView initBaseData];
        _textView.textColor = UIColorFromRGB(0x333333, 1);
        _textView.tintColor = UIColorFromRGB(0x33a928, 1);
        _textView.font = [UIFont systemFontOfSize:15];
        
//        _textView.delegate = self;
        
        _textView.layer.masksToBounds = YES;
        _textView.layer.cornerRadius = 3;
        _textView.layer.borderWidth = 0.6;
        _textView.layer.borderColor = UIColorFromRGB(0xcccccc, 1).CGColor;
        
        _textView.placeholder       = LZLocalizedString(@"please_enter_description", nil);
        _textView.placeholderFont   = [UIFont systemFontOfSize:15];
        _textView.placeholderColor  = UIColorFromRGB(0xcccccc, 1);
        _textView.layoutManager.allowsNonContiguousLayout = NO;
    }
    
    return _textView;
}

//- (UILabel *)placeHolderLabel {
//    
//    if (_placeHolderLabel == nil) {
//        _placeHolderLabel = [[UILabel alloc] init];
//        _placeHolderLabel.font = [UIFont systemFontOfSize:15];
//        _placeHolderLabel.numberOfLines = 0;
//        _placeHolderLabel.textColor = UIColorFromRGB(0xcccccc, 1);
//        [_placeHolderLabel sizeToFit];
//    }
//    
//    return _placeHolderLabel;
//}

- (UILabel *)numberLabel {
    
    if (_numberLabel == nil) {
        _numberLabel = [[UILabel alloc] init];
        _numberLabel.font = [UIFont systemFontOfSize:10];
    }
    
    return _numberLabel;
}

- (void)setCouponListModel:(LZCouponListModel *)couponListModel {

    _couponListModel = couponListModel;
    
    self.titleLabel.text = LZLocalizedString(@"description", @"");
    
    if (couponListModel.profile) {
        self.textView.text = couponListModel.profile;
    }
    else {
        [self.textView setText:nil];
        self.textView.placeholder = LZLocalizedString(@"please_enter_description", nil);
    }
    
    [self setNumberlabelStringForNum:(unsigned long)self.textView.text.length];
}

- (void)setTextViewTag:(NSInteger)tag {
    self.textView.tag = tag;
}

- (void)setNumberlabelStringForNum:(NSInteger)num {
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]
                                      initWithString:[NSString stringWithFormat:@"%ld/1000", (long)num]];
    
    NSRange range1 = [[str string] rangeOfString:[NSString stringWithFormat:@"%ld", (long)num]];
    [str addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x333333, 1) range:range1];
    
    NSRange range2 = [[str string] rangeOfString:@"/1000"];
    [str addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xcccccc, 1) range:range2];
    [self.numberLabel setAttributedText:str];
}

#pragma mark - textViewDelegate

//- (void)textViewDidChange:(UITextView *)textView
//{
//    [self setNumberlabelStringForNum:textView.text.length];
//}
//- (void)textViewDidEndEditing:(UITextView *)textView {
//    
//    [self setNumberlabelStringForNum:textView.text.length];
//    
//    if (self.textViewEditDidEndBlock) {
//        self.textViewEditDidEndBlock(textView.text, textView.tag,self.indexPath);
//    }
//}
//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
//{
//    if (textView == self.textView) {
//        
//        NSString * toBeString = [textView.text stringByReplacingCharactersInRange:range withString:text]; //得到输入框的内容
//        if (toBeString.length > 1000) {
//            return NO;
//        }
//        else {
//            DLog(@"%@, %ld", toBeString, (long)toBeString.length);
//            [self setNumberlabelStringForNum:toBeString.length];
//            return YES;
//        }
//        
////        NSInteger existedLength = textView.text.length;
////        NSInteger selectedLength = range.length;
////        NSInteger replaceLength = text.length;
////        
////        if (existedLength - selectedLength + replaceLength > 1000) {
////            return NO;
////        }
////
////        NSInteger textlength = existedLength - selectedLength + replaceLength;
////        [self setNumberlabelStringForNum:textlength];
//    }
//    
//    return YES;
//}

@end
